package com.xing.server.controller;
/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * demo
 * 2021/11/28 23:57
 */

import com.xing.server.base.R;
import com.xing.server.pojo.Admin;
import com.xing.server.dto.AdminLoginDto;
import com.xing.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * 登录
 *
 * @author: xingfuhao
 * @create: 2021-11-28 23:57
 */
@Api(tags = "LoginController")
@RestController
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public R login(@RequestBody AdminLoginDto adminLoginDto, HttpServletRequest request){
        return adminService.login(adminLoginDto.getUsername(), adminLoginDto.getPassword(),adminLoginDto.getCode(),request);
    }

    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if(null==principal){
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUserName(username);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return admin;
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public R logout(){
        return R.success("注销成功");
    }

}
